package com.bwton.msx.ccb.ui;

import android.content.Context;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.util.Pair;
import android.view.View;

import com.bwton.msx.ccb.R;
import com.bwton.msx.ccb.ui.setting.SettingDefakeFragment;
import com.bwton.msx.ccb.ui.setting.SettingMotionsFragment;
import com.bwton.msx.ccb.ui.setting.SettingThresholdsFragment;

import java.util.ArrayList;
import java.util.List;

public class SettingsActivity extends FragmentActivity {
    List<Pair<String, String>> fragmentArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        fragmentArray = new ArrayList<>();
        fragmentArray.add(new Pair<>("阈值配置", SettingThresholdsFragment.class.getName()));
        fragmentArray.add(new Pair<>("动作配置", SettingMotionsFragment.class.getName()));
        fragmentArray.add(new Pair<>("Defake配置", SettingDefakeFragment.class.getName()));


        TabLayout tabLayout = findViewById(R.id.tabLayout);
        for (Pair<String, String> tab : fragmentArray) {
            tabLayout.addTab(tabLayout.newTab().setText(tab.first));
        }

        final ViewPager viewPager = findViewById(R.id.viewPager);
        viewPager.setAdapter(new PageAdapter(this, getSupportFragmentManager(), fragmentArray));
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        //绑定tab点击事件
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public class PageAdapter extends FragmentPagerAdapter {

        private Context context;

        private List<Pair<String, String>> fragments;

        public PageAdapter(Context context, FragmentManager fm, List<Pair<String, String>> fragments) {
            super(fm);
            this.context = context;
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return Fragment.instantiate(context, fragments.get(position).second, null);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

}